import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * If p is the perimeter of a right angle triangle with integral length sides, 
 * {a,b,c}, there are exactly three solutions for p = 120. 
 * {20,48,52}, {24,45,51}, {30,40,50} 
 * For which value of p ≤ 1000, is the number of solutions maximised?
 */

/**
 * @author TrinhNX
 * @start_date	: 2015/05/07
 * @end_date 	:
 */
public class Euler039 {
	public static void main(String[] args) {
		final int MAX = 1000;
		final int MIN = 12; // { 3, 4 ,5 }
		final List<String> printList = new ArrayList<String>(MAX - MIN);
		int count = 0, temp = 0;
		int temp_count; // store counting value foreach loop
		System.out.println("Start");
		StringBuilder sb = new StringBuilder(10);
		final long start = System.currentTimeMillis();
		for (int i = MIN; i <= MAX; i = i + 2) {
			sb.append(i + "\t");
			// Loop for c, assume c > b >= a [1/3 - 1/2]
			temp_count = 0;
			final int MAX_C = i / 2, MIN_C = i / 3;
			for (int c = MIN_C; c <= MAX_C; c++) {
				// Loop for b [(max-c)/2, c-1]
				for (int b = (MAX_C - c / 2); b < c; b++) {
					// Loop for a, a loop???, noooooo
					int a = (i - b - c);
					if ((a * a + b * b - c * c) == 0) {
						temp_count = temp_count + 1;
						sb.append(a + "\t");
						sb.append(b + "\t");
						sb.append(c + "\t");
					}
				}
			}
			sb.append(temp_count + "\n");
			if (temp_count > count) {
				count = temp_count;
				temp = i;
			}
			printList.add(sb.toString());
			sb.setLength(0); // reset it.
		}
		System.out.println(count + "---" + temp);
		System.out.println("End after: " + (System.currentTimeMillis() - start));
		Common.writeList(printList, new File("C:/java/euler039.txt"));
	}
}
